<#include "/WEB-INF/views/admin/common/layout/__formlayout.html">
<#assign pageCss>
<link rel="stylesheet" href="${ctx}/resource/validationengine/css/validationEngine.jquery.css">
</#assign>
<#assign pageScript>
<script type="text/javascript" src="${ctx}/resource/validationengine/js/jquery.validationEngine-en.js"></script>
<script type="text/javascript" src="${ctx}/resource/validationengine/js/jquery.validationEngine.js"></script>
<script type="text/javascript">
$(document).ready(function() {
	<#if user??>
		$("#type").val(${user.type});
		$("#des").text("${user.des}");
		$("#account").text("${user.account}");
		$("#name").attr("disabled",true)
	</#if>
		
	
	
	$("#frm").validationEngine('attach', {
		promptPosition : 'topLeft',
		scroll : false
	});
	
	$("#role").change(function() {
		var selected = $("#role option:selected").text();
		if(selected == "Customer"){
			$("div[name='trigger']").each(function(){
				$(this).show();
			});
			$("div[name='trigger1']").each(function(){
				$(this).hide();
			});
		}else if(selected == "Agent"){
			$("div[name='trigger1']").each(function(){
				$(this).show();
			});
			$("#parent").val("0");
			$("div[name='trigger']").each(function(){
				$(this).hide();
			});
		}else{
			$("#parent").val("0");
			$("div[name='trigger']").each(function(){
				$(this).hide();
			});
			$("div[name='trigger1']").each(function(){
				$(this).hide();
			});
		}
		
	});
	
	$("#subBtn").click(function() {
		var frm = $("#frm");
		var selected = $("#role option:selected").text();
		var t_parent = $("#parent").val();
		var flag = true;
		if(selected == "Customer" && t_parent == "0"){
			flag=false;
			layer.msg("parent is required", {
				time : 1000,
				icon : 2
			});
		}
		if (frm.validationEngine("validate") && flag) {
			$.ajax({
				type : "POST",
				url : "${ctx}/admin/sys/user/save_update",
				data : frm.serialize(),
				success : function(result) {
					if (result.isSuccess) {
						layer.msg(result.msg, {
							icon : 1,
							time : 1000,
							end : function() {
								//信息提示完成后关闭表单窗口
								var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
								parent.layer.close(index); //再执行关闭  
							}
						});
					} else {
						layer.msg(result.msg, {
							time : 1000,
							icon : 2
						});
					}
				}
			});
		}
	});
});
</script>
</#assign>
<@formlayout>
<div class="row">
	<div class="am-u-sm-12 am-u-md-12 am-u-lg-12" style="width:800px;height:600px">
		<div class="widget am-cf">
			<div class="widget-body am-fr">
				<form class="am-form tpl-form-border-form" id="frm"
					action="${ctx}/admin/sys/res/save">
					<input type="hidden" name="user.id" id="id" value="${user.id!}">
					<div class="am-form-group">
						<label for="name" class="am-u-sm-3 am-form-label">name </label>
						<div class="am-u-sm-9">
							<input type="text"
								class="tpl-form-input validate[required,maxSize[50]]" id="name" value="${user.name!}"
								name="user.name" placeholder="please enter user name">
						</div>
					</div>
					<!-- <div class="am-form-group">
						<label for="email" class="am-u-sm-3 am-form-label">email </label>
						<div class="am-u-sm-9">
							<input type="text"
								class="tpl-form-input validate[required,custom[email],maxSize[50]]" id="email" value="${user.email!}" 
								name="user.email" placeholder="please enter user email">
						</div>
					</div> -->
					<div class="am-form-group">
						<label for="email" class="am-u-sm-3 am-form-label">role </label>
						<div class="am-u-sm-9">
							<select lay-search name="role" id="role" class="am-selected-text validate[required]">
								<option value="">please select role</option>
								<#list roles as role>
									<#if myRole.id == role.id>
										<option value="${role.id}" selected="selected">${role.name}</option>
									<#else>
										<option value="${role.id}">${role.name}</option>
									</#if>
								</#list>
							</select>
						</div>
					</div>
					<div name="trigger" class="am-form-group" style="display:none">
						<label for="parent" class="am-u-sm-3 am-form-label">parent user</label>
						<div class="am-u-sm-9">
							<select data-am-selected="{searchBox: 1}" name="user.parent" id="parent" class="am-selected-text validate[required]">
								<option value="0">select a parent user</option>
								<#list agents as agent>
									<#if user.parent == agent.id>
										<option value="${agent.id}" selected="selected">${agent.name}</option>
									<#else>
										<option value="${agent.id}">${agent.name}</option>
									</#if>
								</#list>
							</select>
						</div>
					</div>
					<div name="trigger1" class="am-form-group" style="display:none">
						<label for="clickNumLimit" class="am-u-sm-3 am-form-label">click num limit</label>
						<div class="am-u-sm-9">
							<input type="text"
								class="tpl-form-input validate[required,custom[number],maxSize[200]]" value="${user.click_num_limit!}"
								id="clickNumLimit" name="user.click_num_limit" placeholder="please enter click number limit">
						</div>
					</div>

                    <div name="trigger1" class="am-form-group" style="display:none">
                        <label for="account" class="am-u-sm-3 am-form-label">account</label>
                        <div class="am-u-sm-9">
                            <textarea class="" rows="10"
                                      id="account" name="user.account" placeholder="please enter user account"></textarea>
                        </div>
                    </div>
					<!-- <div name="trigger" class="am-form-group" style="display:none">
						<label for="appVersion" class="am-u-sm-3 am-form-label">app version</label>
						<div class="am-u-sm-9">
							<input type="text"
								class="tpl-form-input validate[required,maxSize[200]]" value="${user.appVersion!}"
								id="appVersion" name="user.appVersion" placeholder="please enter app version">
						</div>
					</div>
					
					<div name="trigger" class="am-form-group" style="display:none">
						<label for="hardware" class="am-u-sm-3 am-form-label">divice hardware number</label>
						<div class="am-u-sm-9">
							<input type="text"
								class="tpl-form-input validate[required,maxSize[200]]" value="${user.hardware!}"
								id="hardware" name="user.hardware" placeholder="please enter divice hardware number">
						</div>
					</div> -->
					
					<!-- <div class="am-form-group">
						<label for="phone" class="am-u-sm-3 am-form-label">phone </label>
						<div class="am-u-sm-9">
							<input type="text"
								class="tpl-form-input validate[required,custom[phone],maxSize[200]]" value="${user.phone!}"
								id="phone" name="user.phone" placeholder="please enter phone number">
						</div>
					</div> -->

					<!-- <div class="am-form-group">
						<label for="icon" class="am-u-sm-3 am-form-label">头像 </label>
						<div class="am-u-sm-9">
							<input type="text"
								class="tpl-form-input validate[required,maxSize[200]]" id="icon" value="${user.icon!}"
								name="user.icon" >
						</div>
					</div> -->

					<div class="am-form-group">
						<label for="des" class="am-u-sm-3 am-form-label">description</label>
						<div class="am-u-sm-9">
							<textarea class="validate[maxSize[50]]" rows="10" value="${user.des}"
								id="des" name="user.des" placeholder="please enter description information"></textarea>
						</div>
					</div>

					<div class="am-form-group">
						<div class="am-u-sm-9 am-u-sm-push-3">
							<button type="button"
								class="am-btn am-btn-primary tpl-btn-bg-color-success "
								id="subBtn">submit</button>
						</div>
					</div>
				</form>
			</div>
		</div>
	</div>
</div>
</@formlayout>

